package CI.aufgabe2.cip.mynodes;

import CI.aufgabe2.cip.SymbolTable;
import CI.aufgabe2.cip.base.AbstractDescr;
import CI.aufgabe2.cip.descriptor.ProcedureDescr;

public class MyProcedureCallNode extends MyAbstractNode {

	private static final long serialVersionUID = 1L;

	private final MyIdNode ident;
	private final MyActualParametersNode actualParams;

	public MyProcedureCallNode(MyIdNode ident,
			MyActualParametersNode actualParams) {
		this.ident = ident;
		this.actualParams = actualParams;
	}

	@Override
	public void print() {
		trace("ProcedureCallNode ");
		if (ident != null)
			ident.print();
		if (actualParams != null) {
			actualParams.print();
		}
		unindent();
	}
	
	
	public AbstractDescr compile(SymbolTable sym) {
		ProcedureDescr procedure = (ProcedureDescr) sym.getDescr(ident.strval);
		// Paramter auf den Stack
		int counter = 0;
		if(actualParams != null){
			int i=0;
			for(MyFPSectionNode sectionNode : procedure.getParams())
				if(sectionNode.getVarState()){
					if(actualParams.getExpressionList().get(i) instanceof MyIdNode)
						((MyIdNode)actualParams.getExpressionList().get(i)).setVar(true);
				}else{
					i = i + sectionNode.getIdentList().getSize();
				}					
			actualParams.compile(sym);			
		}
		
		write("CALL, "+ procedure.getLabel());
		return null;
	}

}
